package com.coder.rental;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Collections;

/**
 * @author: zhanglin
 * @description:  代码生成器
 * @date 2025/3/16
 */
@SpringBootTest
public class GenaratorCode {
    //定义常量
    private static final String AUTHOR="zhanglin";
    private static final String JDBC_URL="jdbc:mysql://localhost:3306/auto_rental";
    private static final String JDBC_USERNAME="root";
    private static final String JDBC_PASSWORD="123456";
    private static final String OUT_DIR=".\\src\\main\\java"; //生成路径
    private static final String PACKAGE_NAME="com.coder"; //包名
    private static final String MODULE_NAME="rental"; //模块名
    //所需要的表
    private static final String[] TABLES={
        "auto_maker","auto_brand","auto_info","sys_dept","sys_permission",
            "sys_role","sys_user","sys_role_permission","sys_user_role",
            "busi_customer","busi_maintain","busi_violation","busi_order",
            "busi_rental_type"};
    //过滤字符串，不要busi_,sys_
    private static final String[] PREFIX={"busi_","sys_"};
    /*
    * 生成代码的测试方法
    * 使用FastAutoGenerator来自动化生成代码，配置了全局参数，包信息，策略以及模版引擎等。
    * 无需参数，执行后会根据配置生成相应的代码。
    * */
    @Test
    public void generatorCode(){
        //创建代码生成器实例，并配置数据库连接信息
        FastAutoGenerator.create(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD)
                .globalConfig(builder -> {
                    builder.author(AUTHOR)
                            .enableSwagger() //开启swagger支持,需要使用swaggerAPI进行接口测试
                            .outputDir(OUT_DIR);
                })
                .packageConfig(builder -> {
                    builder.parent(PACKAGE_NAME)
                            .moduleName(MODULE_NAME)
                            .pathInfo(Collections.singletonMap(OutputFile.xml, ".\\src\\main\\resources\\mappser"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(TABLES)//生成表的实体类
                            .addTablePrefix(PREFIX) //过滤掉前缀
                            //配置实体类、控制器等的生成选项
                            .entityBuilder()
                            .enableLombok()
                            .enableChainModel()
                            .controllerBuilder()   //生成链
                            .enableRestStyle();
                })
                .templateEngine(new FreemarkerTemplateEngine()) //配置模版引擎
                .execute();

    }
}
